package com.zqxq.lottery.controller.expert;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.lottery.entity.po.export.ExpertInfoPO;
import com.zqxq.lottery.entity.po.sys.SysConfigPO;
import com.zqxq.lottery.entity.vo.expert.CommissionVO;
import com.zqxq.lottery.mapper.order.UserOrderMapper;
import com.zqxq.lottery.mapper.sys.SysConfigMapper;
import com.zqxq.lottery.service.expert.IExpertFollowService;
import com.zqxq.lottery.service.expert.IExpertInfoService;
import com.zqxq.lottery.service.user.IUserCashService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;


@RestController
@Slf4j
public class ExpertController extends ExpertBaseController {

    @Autowired
    private IExpertInfoService iExpertInfoService;

    @Autowired
    private IExpertFollowService iExpertFollowService;

    @Autowired
    private IUserCashService iUserCashService;

    @Autowired
    private UserOrderMapper userOrderMapper;

    @Autowired
    private SysConfigMapper sysConfigMapper;

    @PostMapping("/expertAuth")
    public Result expertAuth(Long user_id,String nick,String head_portrait,String introduce) {
        Result rs = new Result();
        try {
            int ret = iExpertInfoService.expertAuth(user_id,nick,head_portrait,introduce);
            rs.setCode(CodeEnum.SUCCESS.getCode());
            rs.setMsg(CodeEnum.SUCCESS.getMsg());
            rs.setData(String.valueOf(ret));
        } catch (Exception e) {
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("get Expert Error:", e);
        }
        return rs;
    }

    @PostMapping("/follow")
    public Result follow(@RequestParam("expert_id") Long expert_id,@RequestParam("fans_id") Long fans_id,@RequestParam("status") Integer status) {
        Result rs = new Result();
        try {
            int ret = iExpertFollowService.follow(expert_id,fans_id,status);
            rs.setData(String.valueOf(ret));
            if(ret > 0){
                rs.setCode(CodeEnum.SUCCESS.getCode());
                rs.setMsg(CodeEnum.SUCCESS.getMsg());
            }else {
                rs.setCode(CodeEnum.FAIL.getCode());
                rs.setMsg(CodeEnum.FAIL.getMsg());
            }
        } catch (Exception e) {
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("get Expert Error:", e);
        }
        return rs;
    }

    /**
     * 跟单-我的佣金信息
     * @param user_id
     * @return
     */
    @PostMapping("/commissionInfo")
    public Result<CommissionVO> commissionInfo(@RequestParam("user_id") Long user_id) {
        Result rs = new Result();
        try {
            rs.setData(iUserCashService.commissionInfo(user_id));
        } catch (Exception e) {
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("get Expert Error:", e);
        }
        return rs;
    }

    /**
     * 跟单-查询专家信息
     * @param user_id
     * @return
     */
    @PostMapping("/getExpertInfo")
    public Result getExpertInfo(@RequestParam("user_id") Long user_id) {
        Result rs = new Result();
        try {
            ExpertInfoPO expertInfoPO = iExpertInfoService.getExpertInfo(user_id);
            //新认证专家前x单0佣金
            Wrapper wrapper = new EntityWrapper();
            wrapper.eq("user_id",user_id);
            wrapper.eq("follow_status",2);
            wrapper.eq("ticket_status",1);
            Integer count = userOrderMapper.selectCount(wrapper);
            SysConfigPO sysConfigPO = sysConfigMapper.getByKey("PUSH_ORDER_EXPERT_ZERO_COMMISSION");
            SysConfigPO sysConfigBetTime = sysConfigMapper.getByKey("PUSH_ORDER_EXPERT_BET_TIME");
            Integer num = Integer.valueOf(sysConfigPO.getCval());
            if(count<num && expertInfoPO!=null){
                expertInfoPO.setMax(BigDecimal.ZERO);
            }
            if(expertInfoPO!=null){
                expertInfoPO.setRemark(sysConfigBetTime.getCval());
                rs.setData(expertInfoPO);
            }else{
                ExpertInfoPO expertInfo = new ExpertInfoPO();
                expertInfo.setRemark(sysConfigBetTime.getCval());
                rs.setData(expertInfo);
            }

        } catch (Exception e) {
            rs.setCode(CodeEnum.FAIL.getCode());
            rs.setMsg(CodeEnum.FAIL.getMsg());
            log.error("get Expert Error:", e);
        }
        return rs;
    }
}
